package com.zhan.rkforum.service;

import com.zhan.rkforum.bean.DiscussPost;
import com.zhan.rkforum.controller.filter.SensitiveFilter;
import com.zhan.rkforum.dao.DiscussPostMapper;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

@Service
@Transactional
public class DiscussPostServiceImpl implements DiscussPostService {

    @Autowired
    private DiscussPostMapper discussPostMapper;

    @Autowired
    private SensitiveFilter sensitiveFilter;

    @Override
    public List<DiscussPost> selectDiscussPosts(int userId,int orderMode, int offset, int limit) {
        return discussPostMapper.selectDiscussPosts(userId,orderMode,offset,limit);
    }

    @Override
    public int selectDiscussPostRows(int userId) {
        return discussPostMapper.selectDiscussPostRows(userId);
    }

    @Override
    public void addDiscussPost(DiscussPost discussPost) {
        if(ObjectUtils.isEmpty(discussPost)){
            throw new IllegalArgumentException("参数不能为空");
        }
        //过滤
        String filterTitle = sensitiveFilter.filter(HtmlUtils.htmlEscape(discussPost.getTitle()));
        String filterContent = sensitiveFilter.filter(HtmlUtils.htmlEscape(discussPost.getContent()));
        //重新设置
        discussPost.setTitle(filterTitle);
        discussPost.setContent(filterContent);
        discussPostMapper.insertDiscussPost(discussPost);
    }

    @Override
    public DiscussPost findDiscussPostById(int id) {
        return discussPostMapper.selectDiscussPostById(id);
    }

    @Override
    public int updateType(int id, int type) {
        return discussPostMapper.updateType(id, type);
    }

    @Override
    public int updateStatus(int id, int status) {
        return discussPostMapper.updateStatus(id, status);
    }

    @Override
    public void updatePostScore(int postId, double score) {
        discussPostMapper.updatePostScore( postId, score);
    }
}
